#include "MGUI_Color.h"

namespace Myway
{
    const MGUI_Color MGUI_Color::White              = MGUI_Color(1.0f, 1.0f, 1.0f, 1.0f);
    const MGUI_Color MGUI_Color::Red                = MGUI_Color(1.0f, 0.0f, 0.0f, 1.0f);
    const MGUI_Color MGUI_Color::Green              = MGUI_Color(0.0f, 1.0f, 0.0f, 1.0f);
    const MGUI_Color MGUI_Color::Blue               = MGUI_Color(0.0f, 0.0f, 1.0f, 1.0f);
    const MGUI_Color MGUI_Color::Black              = MGUI_Color(0.0f, 0.0f, 0.0f, 1.0f);
    const MGUI_Color MGUI_Color::Gray               = MGUI_Color(0.5f, 0.5f, 0.5f, 1.0f);


	/* :) MGUI_Color
	---------------------------------------------------------------------------------------
		@Remark:
			MGUI_Color is 4 floats (clamp 0.0 - 1.0)
	---------------------------------------------------------------------------------------
	*/
	MGUI_Color::MGUI_Color(float v) : r(v), g(v), b(v), a(v) 
	{
	}

	MGUI_Color::MGUI_Color(float _r, float _g, float _b, float _a) : r(_r), g(_g), b(_b), a(_a) 
	{
	}

	MGUI_Color::MGUI_Color(const MGUI_Color & c) : r(c.r), g(c.g), b(c.b), a(c.a)
	{
	}

	MGUI_Color& MGUI_Color::operator =(const MGUI_Color & c)
	{
		r = c.r, g = c.g, b = c.b, a = c.a;
		return *this;
	}

	MGUI_Color MGUI_Color::operator +(const MGUI_Color & c) const
	{
		return MGUI_Color(r + c.r, g + c.g, b + c.b, a + c.a);
	}

	MGUI_Color MGUI_Color::operator -(const MGUI_Color & c) const
	{
		return MGUI_Color(r - c.r, g - c.g, b - c.b, a - c.a);
	}

	MGUI_Color MGUI_Color::operator *(const MGUI_Color & c) const
	{
		return MGUI_Color(r * c.r, g * c.g, b * c.b, a * c.a);
	}

	MGUI_Color MGUI_Color::operator *(float v) const
	{
		return MGUI_Color(r * v, g * v, b * v, a * v);
	}

	MGUI_Color MGUI_Color::operator /(float v) const
	{
		MGUI_Color c;

		v = 1.0f / v;
		c.a = a * v;
		c.r = r * v;
		c.g = g * v;
		c.b = b * v;

		return c;
	}

	MGUI_Color& MGUI_Color::operator +=(const MGUI_Color & c)
	{
		r += c.r;
		g += c.g;
		b += c.b;
		a += c.a;

		return *this;
	}

	MGUI_Color& MGUI_Color::operator -=(const MGUI_Color & c)
	{
		r -= c.r;
		g -= c.g;
		b -= c.b;
		a -= c.a;

		return *this;
	}

	MGUI_Color& MGUI_Color::operator *=(const MGUI_Color & c)
	{
		r *= c.r;
		g *= c.g;
		b *= c.b;
		a *= c.a;

		return *this;
	}

	MGUI_Color& MGUI_Color::operator *=(float v)
	{
		r *= v;
		g *= v;
		b *= v;
		a *= v;

		return *this;
	}

	MGUI_Color& MGUI_Color::operator /=(float v)
	{
		v = 1.0f / v;

		r *= v;
		g *= v;
		b *= v;
		a *= v;

		return *this;
	}

	bool MGUI_Color::operator ==(const MGUI_Color & c) const
	{
		return a == c.a && r == c.r && g == c.g && b == c.b;
	}

	bool MGUI_Color::operator !=(const MGUI_Color & c) const
	{
		return a != c.a || r != c.r || g != c.g || b != c.b;
	}

	MGUI_Color Myway::operator *(float v, const MGUI_Color & c)
	{
		return c * v;
	}

	void MGUI_Color::Saturate()
	{
		if (r < 0)
			r = 0;
		else if (r > 1)
			r = 1;

		if (g < 0)
			g = 0;
		else if (g > 1)
			g = 1;

		if (b < 0)
			b = 0;
		else if (b > 1)
			b = 1;

		if (a < 0)
			a = 0;
		else if (a > 1)
			a = 1;
			
	}

	TString128 MGUI_Color::ToString() const
	{
		int ir = int(r * 255);
		int ig = int(r * 255);
		int ib = int(r * 255);
		int ia = int(r * 255);

		TString128 str;

		str.Format("%d %d %d %d", ir, ig, ib, ia);

		return str;
	}

	void MGUI_Color::FromString(const TString128 & str)
	{
		int ir = 0, ig = 0, ib = 0, ia = 0;

		sscanf(str.c_str(), "%d %d %d %d", &ir, &ig, &ib, &ia);

		r = ir / 255.0f;
		g = ig / 255.0f;
		b = ib / 255.0f;
		a = ia / 255.0f;
	}
}